import common.CommonDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class WindowHandle {

    private CommonDemo commonDemo;
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeTest
    public void setUp() throws Exception{
        commonDemo = new CommonDemo();
        driver = commonDemo.getChromeDriver();
        String url = commonDemo.getUserDir("/src/main/resources/windowHandle.html");
        driver.get(url);
        driver.manage().window().maximize();
        wait = commonDemo.getWebDriverWait(driver, 5);
    }

    /**
     * 获取window句柄
     */
    @Test
    public void getWindowHandler(){
        String text = driver.getWindowHandle();
        System.out.println(text);
    }

    /**
     * 通过windowHandler切换tab
     */
    @Test
    public void switchToTab(){
        String originalWindow = driver.getWindowHandle();
        assert driver.getWindowHandles().size() == 1;
        //打开新的tab
        driver.findElement(By.linkText("百度新闻")).click();
        wait.until(ExpectedConditions.numberOfWindowsToBe(2));
        for (String windowHandler : driver.getWindowHandles()){
            if(!originalWindow.contentEquals(windowHandler)){
                driver.switchTo().window(windowHandler);
                break;
            }
        }
        wait.until(ExpectedConditions.titleContains("百度新闻"));
        String title = driver.getTitle();
        System.out.println(title);
        //关闭当前窗口window或者标签tab
        driver.close();
        driver.switchTo().window(originalWindow);
    }

    /**
     * 通过windowHandler切换window
     */
    @Test
    public void switchToWindow(){
        String originalWindow = driver.getWindowHandle();
        assert driver.getWindowHandles().size() == 1;
        //打开新的tab
        driver.findElement(By.id("image")).click();
        wait.until(ExpectedConditions.numberOfWindowsToBe(2));
        for (String windowHandler : driver.getWindowHandles()){
            if(!originalWindow.contentEquals(windowHandler)){
                driver.switchTo().window(windowHandler);
                break;
            }
        }
        wait.until(ExpectedConditions.titleContains("百度图片"));
        String title = driver.getTitle();
        System.out.println(title);
        driver.manage().window().maximize();
        //关闭当前窗口window或者标签tab
        driver.close();
        driver.switchTo().window(originalWindow);
    }



    /**
     * 创建新的tab
     */
    @Test
    public void createNewTab(){
        String originalWindow = driver.getWindowHandle();
        driver.switchTo().newWindow(WindowType.TAB);
        //关闭当前窗口window或者标签tab
        driver.close();
        driver.switchTo().window(originalWindow);
    }

    /**
     * 创建新的window
     */
    @Test
    public void createNewWindow(){
        String originalWindow = driver.getWindowHandle();
        driver.switchTo().newWindow(WindowType.WINDOW);
        //关闭当前窗口window或者标签tab
        driver.close();
        driver.switchTo().window(originalWindow);
    }

    @Test
    public void createWindowTab() throws Exception{
        String originalWindow = driver.getWindowHandle();
        driver.switchTo().newWindow(WindowType.TAB);
        Thread.sleep(2000);
        //关闭当前窗口window或者标签tab
        driver.close();
        driver.switchTo().window(originalWindow);
    }

    @AfterTest
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
